const mongoose = require("mongoose");
const AutoIncrement = require("mongoose-sequence")(mongoose);
const dayjs = require("dayjs");

const bannerSchema = new mongoose.Schema({
    title: { type: String, required: true, select: false },
    img: { type: String, required: true },
    url: { type: String, required: true },
    sort: { type: Number, default: 1, select: false },
    status: { type: Number, default: 0, enum: [0, 1], select: false },
    createAt: { type: String, select: false },
});

bannerSchema.pre("save", function (next) {
    if (this.isNew) {
        this.createAt = dayjs().format("YYYY/MM/DD HH:mm:ss");
    }
    next();
});
bannerSchema.plugin(AutoIncrement, { inc_field: "sort" });

module.exports = mongoose.model("Banner", bannerSchema);
